using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;

namespace DriverSVH
{
    public struct SVHHomeSettings
    {
        //! Movement direction of the finger +1 or -1 home in positive or negative direction
        public int direction;
        //! Minimum reachable tick limit, given as offset from the hard stop (soft limit)
        public float minimumOffset;
        //! Maximum reachable tick limt, given as an offset from the hard stop (soft limit)
        public float maximumOffset;
        //! Idle position to move the fingers to after initialization @note This position should be within the min and max offset positions.
        public float idlePosition;
        //! The total angle in [rad] that is used by the finger i.e. the anlge between one (soft) stop and the other This value will be used for calculation of the ticks
        //! @note Currently we use the HARD STOP to HARD STOP angle but map that to a range of ticks from SOFT STOP to SOFT STOP this might be the wrong approach
        //! @note as we therefore would get an angle of zero event hough the finger might be standing at 5k ticks. However by using this method we can asure the Range of the
        //! @note input to be of constant size. This night change to better reflect the real rad value
        public float rangeRad;
        //! The resetCurrentFactor indicates how much of the maximum allowed current (of the controller) must be present in order to decide that a hard stop was reached. @note Values betweeen 0.0 and 1.0 are allowed
        public float resetCurrentFactor;

        public SVHHomeSettings(int direction_,float minimumOffset_,float maximumOffset_,float idlePosition_,float rangeRad_,float resetCurrentFactor_)
        {

            direction = direction_;
            minimumOffset = minimumOffset_;
            maximumOffset = maximumOffset_;
            idlePosition = idlePosition_;
            rangeRad = rangeRad_;
            resetCurrentFactor = resetCurrentFactor_;

            // Limit the resetCurrentFactor to allowed bounds
            resetCurrentFactor = Mathf.Max(Mathf.Min(resetCurrentFactor, 1.0f), 0.0f);
        }
        public SVHHomeSettings(float[] homeSettings)
        {
            int size = homeSettings.Length;
            float temp_direction = (size > 0) ? homeSettings[0] : 0.0f;
            direction = temp_direction < 0.0 ? -1 : +1;
            minimumOffset = (size > 1) ? homeSettings[1] : 0.0f;
            maximumOffset = (size > 2) ? homeSettings[2] : 0.0f;
            idlePosition = (size > 3) ? homeSettings[3] : 0.0f;
            rangeRad = (size > 4) ? homeSettings[4] : 0.0f;
            resetCurrentFactor = (size > 5) ? homeSettings[5] : 0.0f;

            // Limit the resetCurrentFactor to allowed bounds
            resetCurrentFactor = Mathf.Max(Mathf.Min(resetCurrentFactor, 1.0f), 0.0f);
        }
    }

}
